package com.sdlh.demo.concurrent;

import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 测试一下currentHashMap.putIfAbsent()方法是否线程安全
 */
public class ConcurrentHashMapDemo {
    public static void main(String[] args) {
        Map<Long, String> clientMap = new ConcurrentHashMap<>();
        //在空的clientMap中添加一个新的记录
        System.out.println("在空的clientMap中添加一个新的记录");
        System.out.println("添加之前的clientMap: " + clientMap);
        long netId = 1234567L;
        String str1 = "michael";
        String result = clientMap.putIfAbsent(netId, str1);
        System.out.println("查看返回值: " + result);
        System.out.println("添加之后的clientMap: " + clientMap);
        System.out.println();

        //重复添加
        System.out.println("重复添加上一次的记录");
        System.out.println("添加之前的clientMap: " + clientMap);
        String str2 = "Zak";
        String result2 = clientMap.putIfAbsent(netId, str2);
        System.out.println("添加之后的clientMap: " + clientMap);
        System.out.println("查看返回值result: " + result2);
        System.out.println();

    }
}
